package pl.diagnoser.server.persistance.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import pl.diagnoser.client.dto.SymptomDictDTO;
import pl.diagnoser.server.persistance.map.SymptomDict;
import pl.diagnoser.server.persistance.tools.HibernateUtil;

public class SymptomDictDao {
        
        @SuppressWarnings("unchecked")
        public List<SymptomDictDTO> getSymptomsList() {
                Session session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();
                List<SymptomDict> symptomsList = new ArrayList<SymptomDict>(session.createQuery("from SymptomDict").list());
                List<SymptomDictDTO> symptomsListDTO = new ArrayList<SymptomDictDTO>(symptomsList != null ? symptomsList.size() : 0);
                
                if(symptomsList != null) {
                        for (SymptomDict symptoms : symptomsList) {
                                symptomsListDTO.add(createSymptomDictDTO(symptoms));
                        }
                }
                session.getTransaction().commit();
                session.close();
                return symptomsListDTO;
        }
        
        @SuppressWarnings("unchecked")
        public List<String> getSymptomsListAsString() {
                Session session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();
                List<SymptomDict> symptomsList = new ArrayList<SymptomDict>(session.createQuery("from SymptomDict").list());
                List<String> symptomsListString = new ArrayList<String>(symptomsList != null ? symptomsList.size() : 0);
                
                if(symptomsList != null) {
                        for (SymptomDict symptoms : symptomsList) {
                                symptomsListString.add(symptoms.getSymptomName());
                        }
                }
                session.getTransaction().commit();
                session.close();
                return symptomsListString;
        }
        
        public String saveSymptom(String newValue) {
                String ret = "OK";
                Session session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();
            try {
                session.save(new SymptomDict(newValue));
                session.getTransaction().commit();
            } catch(Exception e) {
                ret = e.getMessage();
            } finally {
                        session.close();
            }   
                return ret;
        }
        
        public String removeSymptoms(List<String> symptoms) {
                String ret = "OK";
                Session session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();
            try {
                for(String symtpomName : symptoms) {
                        SymptomDict symptom = (SymptomDict) session.createCriteria(SymptomDict.class).add(Expression.eq("symptomName", symtpomName)).uniqueResult();
                        session.delete(symptom);
                        session.getTransaction().commit();
                }
            } catch(Exception e) {
                ret = e.getMessage();
            } finally {
                        session.close();
            }   
                return ret;
        }
        
        public SymptomDictDTO createSymptomDictDTO( SymptomDict symptomDict ) {
                return new SymptomDictDTO( symptomDict.getSymptomName() );
        }
}
